package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleMapper.add(role);
        Integer roleId = role.getId();
        setRoleAndPermission(roleId, permissionIds);
        setRoleAndMenu(roleId, menuIds);
        findPathsByMenuIds(menuIds,roleId);
    }

    private void findPathsByMenuIds(Integer[] menuIds, Integer roleId) {
        List<String> paths = roleMapper.findPathsByMenuIds(menuIds);
        for (String path : paths) {
            if (path.startsWith("/")) {
                String s1 = path.substring(1);
                String s2 = s1.substring(0, 1);
                if (!paths.contains(s2)) {
                    Integer menuId = roleMapper.findMenuIdByPath(s2);
                    Integer count = roleMapper.findCountByMenuIdAndroleId(roleId,menuId);
                    if (count == 0) {
                        roleMapper.setRoleAndMenu2(roleId, menuId);
                    } else {
                        continue;
                    }
                }
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleMapper.findByCondition(queryString);
        PageInfo pageInfo = new PageInfo(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);

    }

    @Override
    public List<Integer> findPermissionIdsById(Integer id) {
        return roleMapper.findPermissionIdsById(id);

    }

    @Override
    public List<Integer> findMenuIdsById(Integer id) {
        return roleMapper.findMenuIdsById(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        roleMapper.deleteAssociation(roleId);
        roleMapper.deleteAssociation2(roleId);
        setRoleAndPermission(roleId, permissionIds);
        setRoleAndMenu(roleId, menuIds);
        findPathsByMenuIds(menuIds,roleId);
        roleMapper.edit(role);
    }

    @Override
    public void deleteById(Integer id) {
        Integer count = roleMapper.findCountById(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.ROLE_IS_QUOTED);
        }
        roleMapper.deleteAssociation(id);
        roleMapper.deleteAssociation2(id);
        roleMapper.deleteRoleById(id);
    }

    @Override
    public List<Role> queryAll() {

        return roleMapper.queryAll();
    }


    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds.length > 0 && permissionIds != null) {
            roleMapper.setRoleAndPermission(permissionIds, roleId);
        }
    }

    private void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds.length > 0 && menuIds != null) {
            roleMapper.setRoleAndMenu(menuIds, roleId);
        }
    }

}
